package bsc.war.form;

import bsc.ejb.metadata.Comparation;
import bsc.ejb.metadata.Framework;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.FileUtils;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class NewFramework extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {

            Map<String, Object> parameterValues = FileUtils.getMultipartParameters(request);
            byte[] logoFile = parameterValues.get("logo")!= null ? (byte[]) parameterValues.get("logo") : null;
            
            boolean edit = false;
            Long frameworkId = -1L;

            if (NumberUtils.isDigits((String)parameterValues.get("id"))) {
                Long id = Long.valueOf((String)parameterValues.get("id"));
                if (id >= 0) {
                    edit = true;
                    frameworkId = id;
                }
            }

            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            
            Framework framework = edit ? delegate.findFramework(frameworkId) : (Framework) Factory.getBean("Framework");
            framework.setName((String)parameterValues.get("name"));
            framework.setDescription((String)parameterValues.get("description"));
            String valid = (String)parameterValues.get("cbFramework");

            if ("on".equals(valid)) {
                Framework activeFramework = delegate.findFramework(Boolean.TRUE);
                activeFramework.setActive(Boolean.FALSE);
                delegate.updateFramework(activeFramework);
                framework.setActive(Boolean.TRUE);
            }

            if (edit) {
                delegate.updateFramework(framework);
            } else {
                delegate.createFramework(framework);
            }

            String fileName = (String) parameterValues.get("logo_filename");

            if (FileUtils.isImage(fileName)) {
                String logoFileName = FileUtils.generateFilename(framework, FileUtils.FileType.LOGO);
                String logoFileURL = FileUtils.generateFileURL(logoFileName, FileUtils.FileType.LOGO);
                boolean fileSaved = FileUtils.saveFile(logoFile, logoFileName, FileUtils.FileType.LOGO);
                if (!fileSaved && !edit) {
                    delegate.deleteFramework(framework);
                } else {
                    framework.setLogo(logoFileURL);
                    delegate.updateFramework(framework);
                }
            }
            
            if(!edit){
                //Se crean comparation
                Comparation c1 = (Comparation) Factory.getBean("Comparation");
                Comparation c2 = (Comparation) Factory.getBean("Comparation");

                c1.setName("Meta");
                c1.setFramework(framework);
                c1.setType(Comparation.Type.GOAL);

                c2.setName("Real");
                c2.setFramework(framework);
                c2.setType(Comparation.Type.REAL);

                delegate.createComparation(c2);
                delegate.createComparation(c1);
            }
            
            request.setAttribute("id", framework.getIdFramework());
            request.setAttribute("edit", edit);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ModelAndView("newFramework");

    }
}
